home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / O83118 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  6.8 KB  |  293 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6. import java.text.BreakIterator;
  7.  
  8. public class Utilities {
  9.    public static final int drawTabbedText(Segment s, int x, int y, Graphics g, TabExpander e, int startOffset) {
  10.       FontMetrics metrics = g.getFontMetrics();
  11.       int nextX = x;
  12.       char[] txt = s.array;
  13.       int flushLen = 0;
  14.       int flushIndex = s.offset;
  15.       int n = s.offset + s.count;
  16.  
  17.       for(int i = s.offset; i < n; ++i) {
  18.          if (txt[i] == '\t') {
  19.             if (flushLen > 0) {
  20.                g.drawChars(txt, flushIndex, flushLen, x, y);
  21.                flushLen = 0;
  22.             }
  23.  
  24.             flushIndex = i + 1;
  25.             if (e != null) {
  26.                nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
  27.             } else {
  28.                nextX += metrics.charWidth(' ');
  29.             }
  30.  
  31.             x = nextX;
  32.          } else if (txt[i] != '\n' && txt[i] != '\r') {
  33.             ++flushLen;
  34.             nextX += metrics.charWidth(txt[i]);
  35.          } else {
  36.             if (flushLen > 0) {
  37.                g.drawChars(txt, flushIndex, flushLen, x, y);
  38.                flushLen = 0;
  39.             }
  40.  
  41.             flushIndex = i + 1;
  42.             x = nextX;
  43.          }
  44.       }
  45.  
  46.       if (flushLen > 0) {
  47.          g.drawChars(txt, flushIndex, flushLen, x, y);
  48.       }
  49.  
  50.       return nextX;
  51.    }
  52.  
  53.    public static final int getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset) {
  54.       int index = getTabbedTextOffset(s, metrics, x0, x, e, startOffset);
  55.  
  56.       for(int i = s.offset + Math.min(index, s.count - 1); i >= s.offset; --i) {
  57.          char ch = s.array[i];
  58.          if (Character.isWhitespace(ch)) {
  59.             index = i - s.offset + 1;
  60.             break;
  61.          }
  62.       }
  63.  
  64.       return index;
  65.    }
  66.  
  67.    public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException {
  68.       Document doc = c.getDocument();
  69.       Element line = getParagraphElement(c, offs);
  70.       int lineStart = line.getStartOffset();
  71.       int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  72.       String s = doc.getText(lineStart, lineEnd - lineStart);
  73.       if (s != null && s.length() > 0) {
  74.          BreakIterator words = BreakIterator.getWordInstance();
  75.          words.setText(s);
  76.          int wordPosition = offs - lineStart;
  77.          if (wordPosition >= words.last()) {
  78.             wordPosition = words.last() - 1;
  79.          }
  80.  
  81.          words.following(wordPosition);
  82.          offs = lineStart + words.next();
  83.       }
  84.  
  85.       return offs;
  86.    }
  87.  
  88.    public static final Element getParagraphElement(JTextComponent c, int offs) {
  89.       Document doc = c.getDocument();
  90.       if (doc instanceof StyledDocument) {
  91.          return ((StyledDocument)doc).getParagraphElement(offs);
  92.       } else {
  93.          Element map = doc.getDefaultRootElement();
  94.          int index = map.getElementIndex(offs);
  95.          Element paragraph = map.getElement(index);
  96.          return paragraph;
  97.       }
  98.    }
  99.  
  100.    public static final int getPositionAbove(JTextComponent c, int offs, int x) throws BadLocationException {
  101.       int lastOffs = getRowStart(c, offs) - 1;
  102.       int bestSpan = 32767;
  103.       int y = 0;
  104.       Rectangle r = null;
  105.       if (lastOffs >= 0) {
  106.          r = c.modelToView(lastOffs);
  107.          y = r.y;
  108.       }
  109.  
  110.       while(r != null && y == r.y) {
  111.          int span = Math.abs(r.x - x);
  112.          if (span < bestSpan) {
  113.             offs = lastOffs;
  114.             bestSpan = span;
  115.          }
  116.  
  117.          --lastOffs;
  118.          r = lastOffs >= 0 ? c.modelToView(lastOffs) : null;
  119.       }
  120.  
  121.       return offs;
  122.    }
  123.  
  124.    public static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
  125.       int lastOffs = getRowEnd(c, offs) + 1;
  126.       int bestSpan = 32767;
  127.       int n = c.getDocument().getLength();
  128.       int y = 0;
  129.       Rectangle r = null;
  130.       if (lastOffs <= n) {
  131.          r = c.modelToView(lastOffs);
  132.          y = r.y;
  133.       }
  134.  
  135.       while(r != null && y == r.y) {
  136.          int span = Math.abs(x - r.x);
  137.          if (span < bestSpan) {
  138.             offs = lastOffs;
  139.             bestSpan = span;
  140.          }
  141.  
  142.          ++lastOffs;
  143.          r = lastOffs <= n ? c.modelToView(lastOffs) : null;
  144.       }
  145.  
  146.       return offs;
  147.    }
  148.  
  149.    public static final int getPreviousWord(JTextComponent c, int offs) throws BadLocationException {
  150.       Document doc = c.getDocument();
  151.       Element line = getParagraphElement(c, offs);
  152.       int lineStart = line.getStartOffset();
  153.       int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  154.       String s = doc.getText(lineStart, lineEnd - lineStart);
  155.       if (s != null && s.length() > 0) {
  156.          BreakIterator words = BreakIterator.getWordInstance();
  157.          words.setText(s);
  158.          int wordPosition = offs - lineStart;
  159.          if (wordPosition >= words.last()) {
  160.             wordPosition = words.last() - 1;
  161.          }
  162.  
  163.          words.following(wordPosition);
  164.          if (offs == lineStart + words.previous()) {
  165.             words.previous();
  166.             int o = words.previous();
  167.             if (o == -1) {
  168.                offs = lineStart;
  169.             } else {
  170.                offs = lineStart + o;
  171.             }
  172.          }
  173.       }
  174.  
  175.       return offs;
  176.    }
  177.  
  178.    public static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException {
  179.       Rectangle r = c.modelToView(offs);
  180.       int n = c.getDocument().getLength();
  181.       int lastOffs = offs;
  182.  
  183.       for(int y = r.y; r != null && y == r.y; r = lastOffs <= n ? c.modelToView(lastOffs) : null) {
  184.          offs = lastOffs++;
  185.       }
  186.  
  187.       return offs;
  188.    }
  189.  
  190.    public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
  191.       Rectangle r = c.modelToView(offs);
  192.       int lastOffs = offs;
  193.  
  194.       for(int y = r.y; r != null && y == r.y; r = lastOffs >= 0 ? c.modelToView(lastOffs) : null) {
  195.          offs = lastOffs--;
  196.       }
  197.  
  198.       return offs;
  199.    }
  200.  
  201.    public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset) {
  202.       int currX = x0;
  203.       int nextX = x0;
  204.       char[] txt = s.array;
  205.       int n = s.offset + s.count;
  206.  
  207.       for(int i = s.offset; i < n; ++i) {
  208.          if (txt[i] == '\t') {
  209.             if (e != null) {
  210.                nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
  211.             } else {
  212.                nextX += metrics.charWidth(' ');
  213.             }
  214.          } else {
  215.             nextX += metrics.charWidth(txt[i]);
  216.          }
  217.  
  218.          if (x >= currX && x < nextX) {
  219.             if (x - currX < nextX - x) {
  220.                return i - s.offset;
  221.             }
  222.  
  223.             return i + 1 - s.offset;
  224.          }
  225.  
  226.          currX = nextX;
  227.       }
  228.  
  229.       return s.count;
  230.    }
  231.  
  232.    public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset) {
  233.       int nextX = x;
  234.       char[] txt = s.array;
  235.       int n = s.offset + s.count;
  236.  
  237.       for(int i = s.offset; i < n; ++i) {
  238.          if (txt[i] == '\t') {
  239.             if (e != null) {
  240.                nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
  241.             } else {
  242.                nextX += metrics.charWidth(' ');
  243.             }
  244.          } else {
  245.             nextX += metrics.charWidth(txt[i]);
  246.          }
  247.       }
  248.  
  249.       return nextX - x;
  250.    }
  251.  
  252.    public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
  253.       Document doc = c.getDocument();
  254.       Element line = getParagraphElement(c, offs);
  255.       int lineStart = line.getStartOffset();
  256.       int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  257.       String s = doc.getText(lineStart, lineEnd - lineStart);
  258.       if (s != null && s.length() > 0) {
  259.          BreakIterator words = BreakIterator.getWordInstance();
  260.          words.setText(s);
  261.          int wordPosition = offs - lineStart;
  262.          if (wordPosition >= words.last()) {
  263.             wordPosition = words.last() - 1;
  264.          }
  265.  
  266.          offs = lineStart + words.following(wordPosition);
  267.       }
  268.  
  269.       return offs;
  270.    }
  271.  
  272.    public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException {
  273.       Document doc = c.getDocument();
  274.       Element line = getParagraphElement(c, offs);
  275.       int lineStart = line.getStartOffset();
  276.       int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  277.       String s = doc.getText(lineStart, lineEnd - lineStart);
  278.       if (s != null && s.length() > 0) {
  279.          BreakIterator words = BreakIterator.getWordInstance();
  280.          words.setText(s);
  281.          int wordPosition = offs - lineStart;
  282.          if (wordPosition >= words.last()) {
  283.             wordPosition = words.last() - 1;
  284.          }
  285.  
  286.          words.following(wordPosition);
  287.          offs = lineStart + words.previous();
  288.       }
  289.  
  290.       return offs;
  291.    }
  292. }
  293.